/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view.layer;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;
import javax.swing.JPanel;

import soccerscope.model.ClientScene;
import soccerscope.model.Param;
import soccerscope.model.SceneSet;
import soccerscope.model.WorldModel;
import soccerscope.util.Color2;
import soccerscope.util.geom.Point2f;
import soccerscope.view.FieldPane;

public class SayLayer extends Layer implements ItemListener {

	public SayLayer(FieldPane fieldPane, boolean enable) {
		super(fieldPane, enable);
		this.drawLeft = true;
		this.drawRight = true;
	}

	@Override
	public String getLayerName() {
		return "Say";
	}

	JCheckBox left, right;

	@Override
	public JPanel getConfigPanel() {
		JPanel panel = new JPanel();
		this.left = new JCheckBox("Left");
		this.left.setSelected(this.drawLeft);
		this.left.addItemListener(this);
		this.right = new JCheckBox("Right");
		this.right.setSelected(this.drawRight);
		this.right.addItemListener(this);
		panel.add(this.left);
		panel.add(this.right);
		return panel;
	}

	public void itemStateChanged(ItemEvent ie) {
		Object o = ie.getSource();
		this.drawLeft = this.left.isSelected();
		this.drawRight = this.right.isSelected();
		this.fieldPane.repaint();
	}

	private boolean drawLeft;

	private boolean drawRight;

	private int lower = 5;

	private Point2f drawPoint = new Point2f();

	private Point2f drawOffset = new Point2f();

	@Override
	public void draw(Graphics g) {
		if (!this.enable)
			return;

		ClientScene clientScene = this.fieldPane.getScene();
		float magnify = this.fieldPane.getMagnify();
		float playerMagnify = this.fieldPane.getPlayerMagnify();
		// float playerR = Param.PLAYER_SIZE * playerMagnify;

		WorldModel wm = WorldModel.getInstance();
		SceneSet sceneSet = wm.getSceneSet();
		Font f1 = g.getFont();
		Font f2 = f1.deriveFont(22.0f);

		for (int i = 0; i < Param.MAX_PLAYER * 2; i++) {
			if ((this.drawLeft && i < Param.MAX_PLAYER)
					|| (this.drawRight && i >= Param.MAX_PLAYER)) {
				if (clientScene.player[i].isEnable()
						&& clientScene.player[i].sayMessage != null
						&& clientScene.player[i].sayMessage.length() != 0) {
					g.setColor(Color.white);
					this.drawOffset.set(clientScene.player[i].getSize()
							* playerMagnify, 0);
					this.drawPoint.add(clientScene.player[i].pos,
							this.drawOffset);
					g.setFont(f2);
					// drawString(g,
					// clientScene.player[i].sayMessage.substring(0,
					// Math.min(10, clientScene.player[i].sayMessage.length())),
					// drawPoint);
					g.setFont(f1);
				}
			}
		}

		for (int offset = this.lower; offset >= 0; offset--) {
			if (sceneSet.hasScene(clientScene.time - offset)) {
				ClientScene tmpClientScene = (ClientScene) sceneSet
						.getScene(clientScene.time - offset);
				for (int i = 0; i < Param.MAX_PLAYER * 2; i++) {
					if ((this.drawLeft && i < Param.MAX_PLAYER)
							|| (this.drawRight && i >= Param.MAX_PLAYER)) {
						if (tmpClientScene.player[i].isEnable()
								&& tmpClientScene.player[i].sayMessage != null
								&& tmpClientScene.player[i].sayMessage.length() != 0) {
							g.setColor(Color2.mix(Color.white,
									Color2.forestGreen, 2, offset));
							this.drawOffset.set(tmpClientScene.player[i]
									.getSize()
									* playerMagnify, 0);
							this.drawPoint.add(tmpClientScene.player[i].pos,
									this.drawOffset);
							g.setFont(f2);
							// drawString(g,
							// tmpClientScene.player[i].sayMessage.substring(0,
							// Math.min(10,
							// tmpClientScene.player[i].sayMessage.length())),
							// drawPoint);
							g.setFont(f1);

							g.setColor(Color2.mix(tmpClientScene.player[i]
									.getColor().brighter(), Color2.forestGreen,
									2, offset));
							drawCircle(
									g,
									tmpClientScene.player[i].pos,
									tmpClientScene.player[i].getSize()
											* playerMagnify
											* ((float) (offset + 1) / (float) this.lower)
											* 2);
						}
					}
				}
			}
		}
	}

}
